//
//  RegisterViewController.m
//  ClassRoom
//
//  Created by apple on 12-11-20.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "RegisterViewController.h"
#import "ASIHTTPRequest.h"
#import "UrlData.h"
#import "JSONKit.h"
#import "LoginViewController.h"

@interface RegisterViewController ()
 
@end
int status=0;

@implementation RegisterViewController
@synthesize emailTextField;
@synthesize nameTextField;
@synthesize passwordTextField;
@synthesize repasswordTextField;

@synthesize emailCheckImageView;
@synthesize nameCheckImageView;
@synthesize passwordCheckImageView;
@synthesize repasswordCheckImageView;

//@synthesize emailView,nameView,passwordView,repasswordView;
@synthesize emailLable,nameLable,passwordLable,repasswordLable;
//@synthesize emailCheckImageView;
//@synthesize nameCheckImageView;

@synthesize emailImageView,nameImageView,passwordImageView,repasswordImageView;

@synthesize ifEmailEntryisTrue,ifNameEntryisTrue,ifPasswordEntryisTrue,ifRepasswordEntryisTrue; //判断注册信息是否输入正确

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
      
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitle:@"用户注册"];
    self.navigationItem.rightBarButtonItem=[[[UIBarButtonItem alloc]initWithTitle:@"主页" style:UIBarButtonItemStylePlain target:self action:@selector(BackTOMenu)]autorelease];
    _toolBar.tintColor =[UIColor colorWithRed:23.0/250.f green:164.0/250.f blue:66.0/250.f alpha:1.0f];
    [_weiboItem setBackgroundImage:[UIImage imageNamed:@"xinlangweibo"] forState:UIControlEventTouchUpInside barMetrics:UIBarMetricsDefault];
    //设置检查图片 蓝色为不通过 白色为通过
//    emailCheckImageView.image=[UIImage imageNamed:@"蓝色.JPG"];
//    nameCheckImageView.image=[UIImage imageNamed:@"蓝色.JPG"];
//    passwordImageView.image=[UIImage imageNamed:@"蓝色.JPG"];
//    repasswordImageView.image=[UIImage imageNamed:@"蓝色.JPG"];
    
    [self initRegistrView];
    
}

- (void)viewDidUnload
{
    [self setEmailTextField:nil];
    [self setNameTextField:nil];
    [self setPasswordTextField:nil];
    [self setRepasswordTextField:nil];
  
   
    [self setEmailCheckImageView:nil];
    [self setNameCheckImageView:nil];
    [self setPasswordImageView:nil];
    [self setRepasswordImageView:nil];
    [_toolBar release];
    _toolBar = nil;
    [_weiboItem release];
    _weiboItem = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void)initRegistrView{
    ifEmailEntryisTrue=FALSE;
    ifNameEntryisTrue=FALSE;
    ifPasswordEntryisTrue=FALSE;
    ifRepasswordEntryisTrue=FALSE;
    //****邮箱***
    emailTextField.font=[UIFont fontWithName:@"Helvetica" size:24];
    emailTextField.backgroundColor=[UIColor clearColor];
    emailTextField.frame=CGRectMake(120, 98+44, 150, 36);
    [emailTextField setBorderStyle:UITextBorderStyleNone];
    
    emailImageView=[[UIImageView alloc]initWithFrame:CGRectMake(40, 95+44, 235, 42)];
    emailImageView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"labbackground"]];
    [self.view addSubview:emailImageView];
    
    emailLable=[[UILabel alloc]initWithFrame:CGRectMake(48, 100+44, 50, 32)];
    emailLable.text=@"邮  箱";
    emailLable.textColor = [UIColor whiteColor];
    emailLable.font = [UIFont systemFontOfSize:15];
    emailLable.backgroundColor=[UIColor clearColor];
    emailCheckImageView=[[UIImageView alloc]initWithFrame:CGRectMake(285, 107.5+44, 15, 15)];
    [self.view addSubview:emailCheckImageView];
    
    [self.view addSubview:emailTextField];
    [self.view addSubview:emailLable];
 
    
    //****昵称**
    nameTextField.frame=CGRectMake(120, 148+44, 150, 36);
    nameTextField.font=[UIFont fontWithName:@"Helvetica" size:24];
    nameTextField.backgroundColor =[UIColor clearColor];
    [nameTextField setBorderStyle:UITextBorderStyleNone];

    nameImageView=[[UIImageView alloc]initWithFrame:CGRectMake(40, 145+44, 235, 42)];
    nameImageView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"labbackground"]];
    [self.view addSubview:nameImageView];
    
    nameLable=[[UILabel alloc]initWithFrame:CGRectMake(48, 150+44, 50, 32)];
    nameLable.text=@"昵  称";
    nameLable.backgroundColor=[UIColor clearColor];
    nameLable.textColor = [UIColor whiteColor];
    nameLable.font = [UIFont systemFontOfSize:15];
    nameCheckImageView=[[UIImageView alloc]initWithFrame:CGRectMake(285, 157.5+44, 15, 15)];
    [self.view addSubview:nameCheckImageView];
    
    [self.view addSubview:nameTextField];
    [self.view addSubview:nameLable];
    
    
    //****密码***
    passwordTextField.frame=CGRectMake(120, 198+44, 150, 36);
    passwordTextField.font=[UIFont fontWithName:@"Helvetica" size:24];
    passwordTextField.backgroundColor=[UIColor clearColor];
    [passwordTextField setBorderStyle:UITextBorderStyleNone];
    
    passwordImageView=[[UIImageView alloc]initWithFrame:CGRectMake(40, 195+44, 235, 42)];
    passwordImageView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"labbackground"]];
    [self.view addSubview:passwordImageView];
    
    passwordLable=[[UILabel alloc]initWithFrame:CGRectMake(48, 200+44, 50, 32)];
    passwordLable.text=@"密  码";
    passwordLable.backgroundColor=[UIColor clearColor];
    passwordLable.textColor = [UIColor whiteColor];
    passwordLable.font = [UIFont systemFontOfSize:15];
    passwordCheckImageView=[[UIImageView alloc]initWithFrame:CGRectMake(285, 207.5+44, 15, 15)];
    [self.view addSubview:passwordCheckImageView];
    
    [self.view addSubview:passwordTextField];
    [self.view addSubview:passwordLable];
    
    
    //****重复密码***
    repasswordTextField.frame=CGRectMake(120, 248+44, 150, 36);
    repasswordTextField.font=[UIFont fontWithName:@"Helvetica" size:24];
    repasswordTextField.backgroundColor=[UIColor clearColor];
    [repasswordTextField setBorderStyle:UITextBorderStyleNone];
  
    repasswordImageView=[[UIImageView alloc]initWithFrame:CGRectMake(40, 245+44, 235, 42)];
    repasswordImageView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"labbackground"]];
    [self.view addSubview:repasswordImageView];
    
    repasswordLable=[[UILabel alloc]initWithFrame:CGRectMake(40, 250+44, 70, 32)];
    repasswordLable.text=@"重复密码";
    repasswordLable.backgroundColor=[UIColor clearColor];
    repasswordLable.textColor = [UIColor whiteColor];
    repasswordLable.font = [UIFont systemFontOfSize:15];
    repasswordCheckImageView=[[UIImageView alloc]initWithFrame:CGRectMake(285, 257.5+44, 15, 15)];
    [repasswordTextField setBorderStyle:UITextBorderStyleNone];
    [self.view addSubview:repasswordCheckImageView];
    
    [self.view addSubview:repasswordTextField];
    [self.view addSubview:repasswordLable];
    
   
    
}




- (IBAction)EmailEditingFinished{
    if ([emailTextField.text rangeOfString:@"@"].location !=NSNotFound)
    {
        emailCheckImageView.image=[UIImage imageNamed:@"Register_Right_ImageView"];
        ifEmailEntryisTrue=TRUE;
        
    }else {
        emailCheckImageView.image=[UIImage imageNamed:@"Register_Error_ImageView"];
        ifEmailEntryisTrue=FALSE;
    }
     [emailTextField resignFirstResponder];
     NSTimeInterval animationDuration = 0.30f;       
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];       
    CGRect rect = CGRectMake(0, 20, 320, 460);       
    self.view.frame = rect;
   
}


- (IBAction)NameEditingFinished {
    if  (nameTextField.text.length>=6)
    {
        nameCheckImageView.image=[UIImage imageNamed:@"Register_Right_ImageView"];
        ifNameEntryisTrue=TRUE;
    }else {
        nameCheckImageView.image=[UIImage imageNamed:@"Register_Error_ImageView"];
        ifNameEntryisTrue=FALSE;
    }
    
    [nameTextField resignFirstResponder];
    NSTimeInterval animationDuration = 0.30f;       
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];       
    CGRect rect = CGRectMake(0, 20, 320, 460);       
    self.view.frame = rect;
    //[passwordTextField becomeFirstResponder];
}

- (IBAction)PasswordEditingFinished 
{
    if  (passwordTextField.text.length>=6&&passwordTextField.text.length<=16)     {
        passwordCheckImageView.image=[UIImage imageNamed:@"Register_Right_ImageView"];
        ifPasswordEntryisTrue=TRUE;
    }else {
        passwordCheckImageView.image=[UIImage imageNamed:@"Register_Error_ImageView"];
        ifPasswordEntryisTrue=FALSE;
    }    
    [passwordTextField resignFirstResponder];
    [self RePasswordEditingFinished];
    NSTimeInterval animationDuration = 0.30f;       
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];       
    CGRect rect = CGRectMake(0, 20, 320, 460);       
    self.view.frame = rect;
}

- (IBAction)RePasswordEditingFinished 
{
   
    if ([repasswordTextField.text isEqualToString:passwordTextField.text]&& (passwordTextField.text.length>=6&&passwordTextField.text.length<=16)) {
        //改变图片
        repasswordCheckImageView.image=[UIImage imageNamed:@"Register_Right_ImageView"];
        ifRepasswordEntryisTrue=TRUE;
    }else {
        repasswordCheckImageView.image=[UIImage imageNamed:@"Register_Error_ImageView"];
        ifRepasswordEntryisTrue=FALSE;
    }
    
    [repasswordTextField resignFirstResponder];
    NSTimeInterval animationDuration = 0.30f;       
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];       
    CGRect rect = CGRectMake(0, 20, 320, 460);       
    self.view.frame = rect;
   
    }
- (IBAction)RegisterButtonPressed:(UIButton *)sender 
{
    NSString *urlstr=[NSString stringWithFormat:@"%@%@%@%@&%@%@&%@%@",ClassRoom_URL,REGISTER_URL,ClassRoom_EMAIL_URL,emailTextField.text,ClassRoom_PASSWORD_URL,passwordTextField.text,ClassRoom_userName_URL,nameTextField.text];
    NSURL *url = [NSURL URLWithString:urlstr];
    if (!ifEmailEntryisTrue) {
        UIAlertView *emailAlert=[[UIAlertView alloc]initWithTitle:@"邮箱不正确" message:@"请输入正确的邮箱" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [emailAlert show];
        [emailAlert release];
        return;
    }
    if (!ifNameEntryisTrue) {
        UIAlertView *emailAlert=[[UIAlertView alloc]initWithTitle:@"昵称不正确" message:@"请输入正确的昵称" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [emailAlert show];
        [emailAlert release];
        return;
    }
    if (!ifPasswordEntryisTrue) {
        UIAlertView *emailAlert=[[UIAlertView alloc]initWithTitle:@"密码长度不正确" message:@"请输入6-16位密码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [emailAlert show];
        [emailAlert release];
        return;
    }
    if (!ifRepasswordEntryisTrue) {
        UIAlertView *emailAlert=[[UIAlertView alloc]initWithTitle:@"两次密码不一致" message:@"请重新输入" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [emailAlert show];
        [emailAlert release];
        return;
    }

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    
    [request setDelegate:self];
    [request startAsynchronous];//设置为异步请求
    
}

-(IBAction)backgroundTap//点击背景
{
    [emailTextField resignFirstResponder];
    [nameTextField resignFirstResponder];
    [passwordTextField resignFirstResponder];
    [repasswordTextField resignFirstResponder];
    NSTimeInterval animationDuration = 0.30f;       
    
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    
    [UIView setAnimationDuration:animationDuration];       
    
    CGRect rect = CGRectMake(0, 20, 320, 460);       
    
    self.view.frame = rect;
    status=0;
}
- (id)init
{
    self = [super init];
    if (self) {
          
    }
    return self;
}
-(void)BackTOMenu//在二级菜单下返回主页
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [emailTextField release];
    [nameTextField release];
    [passwordTextField release];
    [repasswordTextField release];

 
    [emailCheckImageView release];
    [nameCheckImageView release];
    [passwordImageView release];
    [repasswordImageView release];
    [_toolBar release];
    [_weiboItem release];
    [super dealloc];

}


#pragma mark -

#pragma mark ASIHTTPRequest

- (void)requestFinished:(ASIHTTPRequest *)request{
    NSString *responseString = [request responseString];
    NSDictionary *loginDic = [[responseString objectFromJSONString] retain];
    NSString *error = [loginDic objectForKey:@"error"];
    [self registerDataOpinion:error];
}

- (void)registerDataOpinion:(NSString *)error{
    if ([error intValue] == -2) {
        UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您输入的昵称已存在" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [al show];
        [al release];
    }
    if ([error intValue] == -1) {
        UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"邮箱已注册，请重新输入" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [al show];
        [al release];
    }
    if([error intValue]==0){
        emailSaved = [NSString stringWithFormat:@"%@",emailTextField.text];
        passWordSaved = [NSString stringWithFormat:@"%@",passwordTextField.text];
        
        UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"注册成功!请登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        al.tag = 222;
        [al show];
        [al release];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 222) {
        if (buttonIndex == 0) {
            LoginViewController *login = [[[LoginViewController alloc] init] autorelease];
            login.emailSaved = emailSaved;
            login.passWordSaved = passWordSaved;
            [self presentViewController:login animated:YES completion:nil];
        }
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"%@",error);
}

#pragma mark -

#pragma mark 解决虚拟键盘挡住UITextField的方法
- (void)keyboardWillShow:(NSNotification *)noti

{       
    
    //键盘输入的界面调整       
    
    //键盘的高度
    
//    float height = 216.0;               
//    
//    CGRect frame = self.view.frame;       
//    
//    frame.size = CGSizeMake(frame.size.width, frame.size.height - height);       
//    
//    [UIView beginAnimations:@"Curl"context:nil];//动画开始         
//    
//    [UIView setAnimationDuration:0.30];          
//    
//    [UIView setAnimationDelegate:self];         
//    
//    [self.view setFrame:frame];        
//    
//    [UIView commitAnimations];
//    
}



-(BOOL)textFieldShouldReturn:(UITextField *)textField

{       
    
    // When the user presses return, take focus away from the text field so that the keyboard is dismissed.       
    
//    NSTimeInterval animationDuration = 0.30f;       
//    
//    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];       
//    
//    [UIView setAnimationDuration:animationDuration];       
//    
//    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);  
//    
//    //CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height);
//    
//    self.view.frame = rect;
//    
//    [UIView commitAnimations];
//    
//    [textField resignFirstResponder];
    
    return YES;       
    
}



- (void)textFieldDidBeginEditing:(UITextField *)textField

{       
    
//    CGRect frame = textField.frame;
//    
//    int offset = frame.origin.y + 32+75 - (self.view.frame.size.height - 216.0);//键盘高度216
//    
//    NSTimeInterval animationDuration = 0.30f;               
//    
//    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];               
//    
//    [UIView setAnimationDuration:animationDuration];
//    
//    float width = self.view.frame.size.width;               
//    
//    float height = self.view.frame.size.height;       
//    
//    if(offset > 0)
//        
//    {
//        
//        CGRect rect = CGRectMake(0.0f, -offset,width,height);               
//        
//        self.view.frame = rect;       
//        
//    }       
//    
//    [UIView commitAnimations];   
 
    
    if (status==0)
    {
       
        NSTimeInterval animationDuration2=0.3f;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];       
        [UIView setAnimationDuration:animationDuration2];
        CGRect rect2 = CGRectMake(0, -20-69, 320, 460);
        self.view.frame = rect2;
        [UIView commitAnimations];
        status=1;
    }else {
        NSTimeInterval animationDuration2=0.0f;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];       
        [UIView setAnimationDuration:animationDuration2];
        CGRect rect2 = CGRectMake(0, -20-69, 320, 460);
        self.view.frame = rect2;
        [UIView commitAnimations];
        status=1;
    }         
    
    
}
- (IBAction)backHome:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
